/*
 * DevicesTimer.c
 *
 *      Author: Honrun
 */
#include "gd32w51x.h"
#include "DevicesTimer.h"


int8_t cTimer5Init(void)
{
    timer_parameter_struct timer_initpara = {0};

    rcu_periph_clock_enable(RCU_TIMER5);

    /* deinit a TIMER */
    timer_deinit(TIMER5);
    /* initialize TIMER init parameter struct */
    timer_struct_para_init(&timer_initpara);
    /* TIMER configuration */
    timer_initpara.prescaler         = (SystemCoreClock / 1000000) - 1;
    timer_initpara.period            = 65536 - 1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER5, &timer_initpara);

    nvic_irq_enable(TIMER5_IRQn, 0, 0);

    /* clear interrupt bit */
    timer_interrupt_flag_clear(TIMER5, TIMER_INT_FLAG_UP);
    /* enable the TIMER interrupt */
    timer_interrupt_enable(TIMER5, TIMER_INT_UP);
    /* enable a TIMER */
    timer_enable(TIMER5);

    return 0;
}

int8_t cTimer15Init(void)
{
    timer_parameter_struct timer_initpara = {0};

    /* enable the peripherals clock */
    rcu_periph_clock_enable(RCU_TIMER15);

    /* deinit a TIMER */
    timer_deinit(TIMER15);
    /* initialize TIMER init parameter struct */
    timer_struct_para_init(&timer_initpara);
    /* TIMER configuration */
    timer_initpara.prescaler         = (SystemCoreClock / 1000000) - 1;
    timer_initpara.period            = 1000 - 1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER15, &timer_initpara);

    nvic_irq_enable(TIMER15_IRQn, 3, 0);

    /* clear interrupt bit */
    timer_interrupt_flag_clear(TIMER15, TIMER_INT_FLAG_UP);
    /* enable the TIMER interrupt */
    timer_interrupt_enable(TIMER15, TIMER_INT_UP);
    /* enable a TIMER */
    timer_enable(TIMER15);

    return 0;
}

int8_t cTimer16Init(void)
{
    timer_parameter_struct timer_initpara = {0};

    /* enable the peripherals clock */
    rcu_periph_clock_enable(RCU_TIMER16);

    /* deinit a TIMER */
    timer_deinit(TIMER16);
    /* initialize TIMER init parameter struct */
    timer_struct_para_init(&timer_initpara);
    /* TIMER configuration */
    timer_initpara.prescaler         = 1 - 1;
    timer_initpara.period            = (SystemCoreClock / 10000) - 1;
    timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
    timer_initpara.counterdirection  = TIMER_COUNTER_UP;
    timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
    timer_initpara.repetitioncounter = 0;
    timer_init(TIMER16, &timer_initpara);

    nvic_irq_enable(TIMER16_IRQn, 8, 0);

    /* clear interrupt bit */
    timer_interrupt_flag_clear(TIMER16, TIMER_INT_FLAG_UP);
    /* enable the TIMER interrupt */
    timer_interrupt_enable(TIMER16, TIMER_INT_UP);
    /* enable a TIMER */
    timer_enable(TIMER16);

    return 0;
}
